﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using PMS.SANY.Util;

namespace PMS.SANY.Entity
{
    /// <summary>
    /// 编码规则
    /// </summary>
    public partial class CF_CodeRule : BusEntity
    {
        /// <summary>
        /// 公司Id
        /// </summary>
        public string CompanyId { get; set; }
        /// <summary>
        /// 编码类型
        /// </summary>
        public string CodeTypeId { get; set; }

        /// <summary>
        /// 规则类型
        /// </summary>
        public string RuleType { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; set; }

        /// <summary>
        /// 规则
        /// </summary>
        public string Rule { get; set; }

        /// <summary>
        /// 长度
        /// </summary>
        public int Length { get; set; }
    }

    public partial class CF_CodeRule : BusEntity
    {
        /// <summary>
        /// 仓库
        /// </summary>
        //public Bas_Whse Whse { get; set; }
        public CF_CodeType CodeType { get; set; }
    }

    /// <summary>
    /// 实体配置
    /// OnModelCreating
    /// </summary>
    public class CF_CodeRuleTypeConfig : BusEntityTypeConfig<CF_CodeRule>, IEntityTypeConfiguration<CF_CodeRule>
    {
        public override void Configure(EntityTypeBuilder<CF_CodeRule> builder)
        {
            base.Configure(builder);

            #region 主外键关系
            //builder.HasOne(o => o.Whse).WithMany().HasForeignKey(p => p.WhseId).OnDelete(DeleteBehavior.ClientCascade);
            builder.HasOne(m => m.CodeType).WithMany(m => m.CodeRules).HasForeignKey(p => p.CodeTypeId);
            #endregion

            #region 字段属性:最大长度,是否必需,默认值
            builder.Property(p => p.CodeTypeId).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.RuleType).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired();
            builder.Property(p => p.Sort).IsRequired().HasDefaultValue(0);
            builder.Property(p => p.Rule).HasMaxLength(EntityDefinition.DefSmallColLen).IsRequired().HasDefaultValue("");
            builder.Property(p => p.Length).IsRequired().HasDefaultValue(0);
            #endregion

            #region 备注
            builder.HasComment("编码规则");
            builder.Property(p => p.CodeTypeId).HasComment("编码类型");
            builder.Property(p => p.RuleType).HasComment("规则类型");
            builder.Property(p => p.Sort).HasComment("排序");
            builder.Property(p => p.Rule).HasComment("规则");
            builder.Property(p => p.Length).HasComment("长度");
            #endregion

            #region 种子数据
            builder.HasData(
                new CF_CodeRule() { Id = $"{EntityDefinition.DefSysCode}_CF_User_Code01", CompanyId = EntityDefinition.DefSysCode, CodeTypeId = $"{EntityDefinition.DefSysCode}_CF_User_Code", RuleType = "Const", Sort = 1, Rule = "U", Length = 1, CreateUserId = "1", ModifyUserId = "1" },
                new CF_CodeRule() { Id = $"{EntityDefinition.DefSysCode}_CF_User_Code02", CompanyId = EntityDefinition.DefSysCode, CodeTypeId = $"{EntityDefinition.DefSysCode}_CF_User_Code", RuleType = "Serial", Sort = 2, Rule = "", Length = 4, CreateUserId = "1", ModifyUserId = "1" }
            );            
            #endregion
        }
    }
}
